import dayjs from 'dayjs';

const typeMappings = {
  string: {
    from: ['text', 'ntext', 'nvarchar', 'varchar', 'nvarchar2', 'varchar2', 'char', 'nchar'],
    nullable: ''
  },
  long: {
    from: ['bigint'],
    nullable: '?'
  },
  int: {
    from: ['int', 'enum', 'integer'],
    nullable: '?'
  },
  short: {
    from: ['smallint'],
    nullable: '?'
  },
  float: {
    from: ['real'],
    nullable: '?'
  },
  double: {
    from: ['float'],
    nullable: '?'
  },
  decimal: {
    from: ['decimal', 'money', 'numeric', 'smallmoney'],
    nullable: '?'
  },
  byte: {
    from: ['tinyint'],
    nullable: '?'
  },
  'byte[]': {
    from: ['image', 'binary', 'varbinary'],
    nullable: '?'
  },
  bool: {
    from: ['bit'],
    nullable: '?'
  },
  Guid: {
    from: ['uniqueidentifier'],
    nullable: '?'
  },
  DateTime: {
    from: ['date', 'datetime', 'datetime2', 'smalldatetime', 'timestamp'],
    nullable: '?'
  }
};

const workflowStatus = [
  { key: -1, value: '草稿' },
  { key: 0, value: '流转结束' },
  { key: 1, value: '用户取消' },
  { key: 2, value: '管理员取消' },
  { key: 3, value: '流转中' },
  { key: 4, value: '流转中(曾被拒绝过)' },
  { key: 5, value: '未流转' },
  { key: 6, value: '表单失效' }
];

function setCaretPosition (textDom, pos) {
  if (textDom.setSelectionRange) {
    textDom.focus();
    textDom.setSelectionRange(pos, pos);
  } else if (textDom.createTextRange) {
    var range = textDom.createTextRange();
    range.collapse(true);
    range.moveEnd('character', pos);
    range.moveStart('character', pos);
    range.select();
  }
}

export function resolveParams (url) {
  url = url || '';

  let dict = new Map();

  if (url.indexOf('?') == -1) {
    return dict;
  }

  let queryString = url.substr(url.indexOf('?'));
  let params = queryString.split('&');

  for (let p of params) {
    let segment = p.split('=');

    dict.set(decodeURIComponent(segment[0]), decodeURIComponent(segment[1]));
  }

  return dict;
}

/**
 * 代码类型
 */
export function codeType (type, nullable, length) {
  let rs = '';
  let props = Object.keys(typeMappings);

  if (type.includes('(')) {
    type = type.substr(0, type.indexOf('('));
  }

  type = type.toLowerCase();

  for (let prop of props) {
    if (typeMappings[prop].from.includes(type)) {
      rs = prop + ((nullable == 1 || nullable == 'YES') ? typeMappings[prop].nullable : '');
    }
  }

  return rs || 'string';
}

export function pascal (value) {
  let rs = '';
  let items = (value || '').split('_');

  for (let item of items) {
    if (item && item.length > 1) {
      rs += item[0].toUpperCase() + item.substr(1);
    } else if (item) {
      rs += item[0].toUpperCase();
    }
  }

  return rs;
};

export function getWorkflowText (item) {
  return (workflowStatus.find(w => w.key == item) || { vlaue: '状态未定义' }).value;
}

export function fillDatas (target, sources) {
  let tkeys = Object.keys(target);

  tkeys.forEach(e => delete target[e]);

  if (sources) {
    let skeys = Object.keys(sources);

    skeys.forEach(e => target[e] = sources[e]);
  }
}

export function friendlyNumbers (number) {
  if (Number.isNaN(number)) {
    return '--';
  }

  let num = parseFloat(number);

  if (Number.isNaN(num)) {
    return '--';
  }

  let absNum = Math.abs(num);

  if (absNum <= 10000) {
    return num;
  } else if (absNum > 10000 && absNum <= 100000000) {
    return (num / 10000).toFixed(2) + '万';
  } else {
    return (num / 100000000).toFixed(4) + '亿';
  }
}

export function replaceWhitespace (p) {
  return (p || ' ').replace(/ /ig, '&nbsp;');
}

export function friendlyVolume (size) {
  let items = ((size || 0) + '').split(' ')

  if (items.length > 1 && items[1].toLowerCase() == 'kb') {
    size = parseFloat(items[0]) * 1024;
  } else if (items.length > 1 && items[1].toLowerCase() == 'mb') {
    size = parseFloat(items[0]) * 1024 * 1024;
  } else if (items.length > 1 && items[1].toLowerCase() == 'gb') {
    size = parseFloat(items[0]) * 1024 * 1024 * 1024;
  }

  if (Number.isNaN(size)) {
    return '--';
  }

  let num = parseFloat(size);

  if (Number.isNaN(num)) {
    return '--';
  }

  if (num < 1024) {
    return num + ' Bytes';
  } else if (num >= 1024 && num < Math.pow(1024, 2)) {
    return (num / 1024).toFixed(2) + ' KB';
  } else if (num >= Math.pow(1024, 2) && num < Math.pow(1024, 3)) {
    return (num / Math.pow(1024, 2)).toFixed(2) + ' MB';
  } else if (num >= Math.pow(1024, 3) && num < Math.pow(1024, 4)) {
    return (num / Math.pow(1024, 3)).toFixed(2) + ' GB';
  } else {
    return (num / Math.pow(1024, 4)).toFixed(2) + ' TB';
  }
}

export function friendlyDate (date) {
  if (!date) {
    return '--';
  }

  return dayjs(date).format('YYYY-MM-DD');
}

export function friendlyDateTime (date) {
  if (!date) {
    return '--';
  }

  return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
}

export function getDate () {
  return dayjs().format('YYYY-MM-DD');
}

export function getDateTime () {
  return dayjs().format('YYYY-MM-DD HH:mm:ss');
}